import unittest, requests, json, operator
from time import sleep
from collections import defaultdict, OrderedDict

class MockTest(unittest.TestCase):

    global AccessToken

    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        print("\n------测试结束------")

    def setUp(self):
        self.url = 'http://localhost:8500'
        self.headers = {"Version": "1450", "Platform": "ios", "Content-Type": "application/json"}

    def tearDown(self):
        pass

    def getToken(self):
        '''登录成功后获取token'''
        global AccessToken
        url = 'http://uat-sj-gateway.aihuishou.com/sj-api/auth/login'
        headers = {"Version": "1450", "Platform": "ios", "Content-Type": "application/json"}
        data = {"userName": "18800000001", "passWord": "qwer1234"}
        data = json.dumps(data)
        r = requests.post(url, headers=headers, data=data)
        # print(r.text)
        token = r.json()['data']['accessToken']
        print("token : " + token)
        AccessToken = token
        headers['Access-Token'] = AccessToken
        print(headers)

        jsObj = json.dumps(headers)
        fileObject = open('headers.json', 'w')
        fileObject.write(jsObj)
        fileObject.close()

    def test_search(self, url='/search'):
        '''验证代拍单商品列表接口'''
        print("---")
        global AccessToken
        data={
            "supplyOrderNo": "S20190111200753075374",
            "pageSize": 20,
            "pageIndex": 0
        }
        headers = {"Version": "1450", "Platform": "ios", "Content-Type": "application/json"}
        headers['Access-Token'] = AccessToken
        print(headers)
        r = requests.post(self.url+url, headers=headers, json=data)
        print("aaaaa")
        print(r)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.json()['code'], 200)

if __name__ == '__main__':
    MockTest().getToken()
    sleep(1)
    unittest.main(verbosity=2)

